﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace RelationExtraction
{
    public partial class MainForm : Form
    {
        public class relation
        {
            public String id1;
            public String id2;
            public String entity1;
            public String entity2;
            public String text;
            public String rela;
        }

        private Common common;
        public DataTable dt = new DataTable("result");
        public List<relation> resultList = new List<relation>();

        public MainForm()
        {
            InitializeComponent();
            common = new Common(this);
            comboBoxRela.SelectedIndex = 0;
            DataColumn newDC;
            newDC = new DataColumn("ID1", System.Type.GetType("System.String"));
            dt.Columns.Add(newDC);
            newDC = new DataColumn("Entity1", System.Type.GetType("System.String"));
            dt.Columns.Add(newDC);
            newDC = new DataColumn("Relation", System.Type.GetType("System.String"));
            dt.Columns.Add(newDC);
            newDC = new DataColumn("ID2", System.Type.GetType("System.String"));
            dt.Columns.Add(newDC);
            newDC = new DataColumn("Entity2", System.Type.GetType("System.String"));
            dt.Columns.Add(newDC);
            newDC = new DataColumn("Text", System.Type.GetType("System.String"));
            dt.Columns.Add(newDC);
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {

        }
        private void addToResList(DataRow data)
        {
            relation rela = new relation();
            rela.id1 = data["ID1"].ToString();
            rela.entity1 = data["Entity1"].ToString();
            rela.id2 = data["ID2"].ToString();
            rela.text = data["Text"].ToString();
            rela.entity2 = data["Entity2"].ToString();
            rela.rela = data["Relation"].ToString();
            resultList.Add(rela);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            String relationName = "";
            int relationId = 0;
            String fileName = "";
            relationId = comboBoxRela.SelectedIndex + 1;
            switch (comboBoxRela.SelectedIndex)
            {
                case 0: relationName = "演唱者"; fileName = "resultdata\\out" + relationId + ".txt"; break;
                case 1: relationName = "作词"; fileName = "resultdata\\out" + relationId + ".txt"; break;
                case 2: relationName = "作曲"; fileName = "resultdata\\out" + relationId + ".txt"; break;
                case 3: relationName = "演员"; fileName = "resultdata\\out" + relationId + ".txt"; break;
                case 4: relationName = "导演"; fileName = "resultdata\\out" + relationId + ".txt"; break;
            }
            resultGridView.DataSource = null;
            resultGridView.Enabled = false;
            dt.Clear();
            resultList.Clear();

            if (File.Exists(fileName) == false)
            {
                MessageBox.Show("结果文件不存在，请先进行关系抽取！");
                return;
            }

            int idx = 0;
            StreamReader sr = new StreamReader(fileName, Encoding.UTF8);
            while (!sr.EndOfStream)
            {
                idx++;
                String line = sr.ReadLine();
                String[] info = line.Split('$');
                DataRow newDR = dt.NewRow();
                newDR["ID1"] = info[0];
                newDR["Entity1"] = info[1];
                newDR["Relation"] = relationName;
                newDR["ID2"] = info[2];
                newDR["Entity2"] = info[3];
                newDR["Text"] = info[4];
                addToResList(newDR);
                dt.Rows.Add(newDR);
                //if (idx > 100)
                //break;
            }
            label1.Text = "共找到 " + idx + "个关系";
            resultGridView.DataSource = dt;
            resultGridView.Enabled = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (comboBoxRela.SelectedIndex == -1)
            {
                MessageBox.Show("请选择要抽取和展示的关系！");
                return;
            }
            Thread thread1 = new Thread(new ParameterizedThreadStart(common.StartExtract));
            thread1.IsBackground = true;
            thread1.Start(comboBoxRela.SelectedIndex + 1);
            buttonEx.Enabled = false;
            UseWaitCursor = true;
        }

        private void resultGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            String str = resultGridView.Rows[e.RowIndex].Cells[5].Value.ToString();
            label6.Text = resultGridView.Rows[e.RowIndex].Cells[4].Value.ToString();
            int pos = 0;
            String rule = "";
            while (pos != -1)
            {
                pos = str.IndexOf('[', pos);
                if (pos == -1)
                    break;
                int end = str.IndexOf(']', pos);
                rule += str.Substring(pos, end - pos + 1);
                pos += 1;
            }
            label5.Text = rule;
            richTextBox1.Text = str;
        }

        private void label7_Click(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        private DataRow relation2DataRow(relation rela)
        {
            DataRow newDR = dt.NewRow();
            newDR["ID1"] = rela.id1;
            newDR["Entity1"] = rela.entity1;
            newDR["Relation"] = rela.rela;
            newDR["ID2"] = rela.id2;
            newDR["Entity2"] = rela.entity2;
            newDR["Text"] = rela.text;
            return newDR;
        }
        private void buttonSearch_Click(object sender, EventArgs e)
        {
            String keyword = textBoxSearchText.Text;
            dt.Clear();
            int findNum = 0;
            foreach (relation rela in resultList)
            {
                if (rela.entity1.Contains(keyword) || rela.entity2.Contains(keyword))
                {
                    findNum++;
                    DataRow newDR = relation2DataRow(rela);
                    dt.Rows.Add(newDR);
                }
            }
            label1.Text = "共找到 " + findNum + "个关系";
        }
    }
}
